package com.xjc.config;

import com.xjc.listener.ProcessTaskListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.common.engine.impl.event.FlowableEventDispatcherImpl;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * <p>
 * Description
 * </p>
 *
 * @author xiaojincai
 * @since 2025-07-10
 */
@Configuration
public class FlowableConfig {

    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration(DataSource dataSource,
                                                                       PlatformTransactionManager transactionManager) {

        final SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration();
        config.setDataSource(dataSource);
        config.setTransactionManager(transactionManager);

        // 初始化事件分发器
        final FlowableEventDispatcher eventDispatcher = new FlowableEventDispatcherImpl();
        config.setEventDispatcher(eventDispatcher);

        // 添加自定义配置
        config.getEventDispatcher().addEventListener(new ProcessTaskListener());
        return config;
    }

}
